<template>
  <!--选择分组-->
  <el-dialog
    :title="params.title"
    :visible.sync="params.visible"
    width="500px"
  >
    <compTree
      ref="tree"
      :tree-default-expand-all="true"
      comp-tree-height="400"
      :show-search="true"
      :show-menu="false"
      :show-right-tag="false"
      :tree-show-checkbox="true"
      :tree-data="treeData"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
import { getLearnGroupList, getLearnTagList } from '@/api/learnCenter'
import { arrayToTree } from '@/utils'
export default {
  name: 'MovePacket',
  props: {
    params: {
      type: Object,
      default: () => {
        return {
          ids: [], // 需要回显的数据
          visible: false,
          type: 'type' // 类型/标签
        }
      }
    }
  },
  data() {
    return {
      // 分组列表
      treeData: [],
      // 要移动到的组
      movedItem: {}
    }
  },
  created() {
    if (this.params.type === 'type') this.getTypeList()
    else this.getLabelList()
  },
  methods: {
    // 获取类型列表
    getTypeList() {
      const vPar = new this.ParamArr()
      getLearnGroupList(vPar).then(vPar => {
        const nodes = []
        const table = new this.SQLTable(vPar.getTable('k_learn_group'))
        table.first()
        while (!table.eof()) {
          const node = {}
          node['id'] = table.getValueByName('ID')
          node['pid'] = table.getValueByName('PID')
          node['text'] = table.getValueByName('Name')
          node['iType'] = table.getValueByName('iType')
          if (node['iType'] === '0') {
            node['icon'] = 'icon-wenjianyi'
          } else {
            node['icon'] = 'icon-wenbenmoban'
          }
          nodes.push(node)
          table.next()
        }
        this.treeData = arrayToTree(nodes, { children: 'nodes' })
        this.$nextTick(() => {
          this.params.ids.length && this.$refs.tree.setCheckedKeys(this.params.ids, true)
        })
      })
    },
    // 获取标签
    getLabelList() {
      const vPar = new this.ParamArr()
      getLearnTagList(vPar).then(vPar => {
        const nodes = []
        const table = new this.SQLTable(vPar.getTable('k_learn_tag'))
        table.first()
        while (!table.eof()) {
          const node = {}
          node['id'] = table.getValueByName('ID')
          node['pid'] = table.getValueByName('PID')
          node['text'] = table.getValueByName('Name')
          node['iType'] = table.getValueByName('iType')
          if (node['iType'] === '0') {
            node['icon'] = 'icon-wenjianyi'
          } else {
            node['icon'] = 'icon-wenbenmoban'
          }
          nodes.push(node)
          table.next()
        }
        this.treeData = arrayToTree(nodes, { children: 'nodes' })
        this.$nextTick(() => {
          this.params.ids.length && this.$refs.tree.setCheckedKeys(this.params.ids, true)
        })
      })
    },
    // 确定
    confirm() {
      const ndoes = this.$refs.tree.getCheckedNodes().filter(item => item.iType === '1')
      if (!ndoes.length) return this.$message.warning(`请勾选${this.params.type === 'type' ? '分类' : '标签'}`)
      this.params.confirm(ndoes)
    }
  }
}
</script>

<style scoped lang="scss">

</style>
